package com.lantu.wisdom.pdf;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
import com.lantu.wisdom.dto.AnswerDTO;
import lombok.SneakyThrows;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;

/**
 * @author ZCH
 * @Description 生成咨询书
 * @since 2021/7/8$ 15:14$
 */
public class CreatePdfUtils {


    public CreatePdfUtils() throws IOException, DocumentException {

    }

    @SneakyThrows
    public static void main(String[] args) {
        AnswerDTO answerDTO = new AnswerDTO();
        answerDTO.setContext("被鉴定人王恒本次外伤，建议其误工期评定为180天，护理期评定为60日，营养期评定为60日，被鉴定人王恒后续治疗费，约需两千元（￥2000.00）");
        answerDTO.setCreateDate(1625818923944L);
        answerDTO.setDescription("建议其误工期评定为180天，护理期评定为60日，营养期评定为60日");
        answerDTO.setStandard("GA/T1193-2014《人身损害误工期、护理起、营养期评定规范》");
        CreatePdfUtils.createPdf(answerDTO);
    }

    public static void createPdf(AnswerDTO context) {

        BaseFont SongDynastyFont = null;
        try {
          /*   String os = System.getProperties().getProperty("os.name");
            String prefixFont = "";
            if (os.startsWith("win") || os.startsWith("Win")) {
                prefixFont = "C:\\Windows\\Fonts" + File.separator;
            } else {
                prefixFont = "/usr/share/fonts/china" + File.separator;
            }*/
            //SongDynastyFont = BaseFont.createFont("/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            SongDynastyFont = BaseFont.createFont("C:\\Windows\\Fonts\\simfang.ttf", BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);

            Font TileText = new Font(SongDynastyFont, 24, Font.BOLD);
            Font textFont = new Font(SongDynastyFont, 16, Font.NORMAL);
            Font fengFont = new Font(SongDynastyFont, 20, Font.BOLD);

            String filename = "咨询意见书";
            // 设置下载格式为pdf
            //response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8") + ".pdf");
            //response.setContentType("application/octet-stream;charset=UTF-8");

            //OutputStream oss = new BufferedOutputStream(response.getOutputStream());
            Document document = new Document(PageSize.A4, 100, 100, 40, 40);
            //PdfWriter.getInstance(document, oss);
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\tools\\test.pdf"));
            // 3.打开文档
            document.open();
            // 4.添加一个内容段落
            document.addTitle("咨询意见书");
            document.addAuthor("陕西");

            // under.setTextMatrix(30, 30);

            //文字居中陕西省人民法院 :
            Paragraph title = new Paragraph("咨询意见书", TileText);
            title.setAlignment(1);
            title.setLeading(60f);
            title.setSpacingAfter(30f);
            document.add(title);
            Paragraph oneLine = new Paragraph("声明", textFont);
            oneLine.setAlignment(0);
            oneLine.setSpacingAfter(12f);
            document.add(oneLine);
            Paragraph oneText = new Paragraph("1、该咨询意见书是根据您上传诊断得来，由于其中没有法医活体检查依据，咨询意见只做参考，咨询人或者使用人应自行承担使用所产生的任何责任及风险。        2、如果要出具正式意见书，请联系平台。", textFont);
            oneText.setFirstLineIndent(42);
            oneText.setSpacingAfter(20f);
            document.add(oneText);



            Paragraph fenxi = new Paragraph("分析说明", fengFont);
            fenxi.setLeading(32f);
            fenxi.setAlignment(Element.ALIGN_LEFT);
            fenxi.setSpacingAfter(20f);
            document.add(fenxi);
            //自定义内容
            Paragraph fenxiDetail = new Paragraph("在您提供资料属实的基础上，依据"+context.getStandard()+"，综合分析如下:" , textFont);
            fenxiDetail.setFirstLineIndent(42);
            fenxiDetail.setLeading(32f);
            fenxiDetail.setSpacingAfter(20f);
            document.add(fenxiDetail);
            String [] des = context.getDescription().split("&");
            for(int i =0;i<des.length;i++){
                Paragraph fenxi1 = new Paragraph(des[i] , textFont);
                fenxi1.setFirstLineIndent(32);
                fenxi1.setLeading(32f);
                fenxi1.setSpacingAfter(20f);
                document.add(fenxi1);
            }

            Paragraph opinionTitleText = new Paragraph("鉴定意见", fengFont);
            opinionTitleText.setLeading(32f);
            opinionTitleText.setAlignment(Element.ALIGN_LEFT);
            opinionTitleText.setSpacingAfter(20f);
            document.add(opinionTitleText);

            //自定义内容
            Paragraph opinionText = new Paragraph(context.getContext(), textFont);
            opinionText.setFirstLineIndent(42);
            opinionText.setLeading(32f);
            opinionText.setSpacingAfter(20f);
            document.add(opinionText);


            //pdf底部
            Paragraph riskTitleText = new Paragraph("风险提示与声明", fengFont);
            riskTitleText.setLeading(32f);
            riskTitleText.setSpacingAfter(20f);
            riskTitleText.setAlignment(Element.ALIGN_LEFT);
            document.add(riskTitleText);
            Paragraph riskTextOne = new Paragraph("1、 本咨询意见书中的任何结论系根据咨询人提供的信息，结合对相关标准的理解作出，不代表法院最后的裁判结果。", textFont);
            riskTextOne.setLeading(32f);
            riskTextOne.setAlignment(Element.ALIGN_LEFT);
            document.add(riskTextOne);
            Paragraph riskTextTwo = new Paragraph("2、 本咨询意见书内容仅供参考，不作任何担保，咨询人或者使用人应自行承担使用所产生的任何责任及风险。", textFont);
            riskTextTwo.setLeading(32f);
            riskTextTwo.setAlignment(Element.ALIGN_LEFT);
            document.add(riskTextTwo);
            Paragraph riskTextThree = new Paragraph("智慧鉴定平台", textFont);
            riskTextThree.setLeading(40f);
            riskTextThree.setAlignment(Element.ALIGN_RIGHT);
            document.add(riskTextThree);
            Paragraph riskTextFour = new Paragraph( DateUtil.format(new Date(context.getCreateDate()), DatePattern.CHINESE_DATE_FORMAT), textFont);
            riskTextFour.setLeading(30f);
            riskTextFour.setAlignment(Element.ALIGN_RIGHT);

            document.add(riskTextFour);
            //oss.flush();
            document.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
